package com.gxuwz.medical.domain.family;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.FamilyDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.vo.PageBean;

public class Family {
	/*
	 * 户基本信息
	 */
	private String familycode;// 家庭编号
	private String familyattribute;// 户属性 一般农户|五保户|贫困户|特困户|军烈属|其他
	private int familynum=1;// 家庭人口数
	private int farmernum=1;// 农业人口数
	private String town;// 镇
	private String village;// 村
	private String group;// 组
	private String address;// 家庭住址
	private Date buildtime;// 建档时间
	private String username;// 登记员
	/*
	 * 户主基本信息
	 */
	private Person holder;
	private FamilyDao familydao = new FamilyDao();
	
	public Family(){}
	public Family(String familyattribute, 
			String town, String village, String group, String address,
			String username) {
		super();
		this.familyattribute = familyattribute;
		this.town = town;
		this.village = village;
		this.group = group;
		this.address = address;
		this.username = username;
	}
	
	

	private void createFamilycode() {
		Connection conn = null;
		Statement stmt=null;
		ResultSet rs =null;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql ="select familycode from family where familycode like'"+this.group+"%'";
			rs = stmt.executeQuery(sql);
			String familycodestr="";
			if(rs.next()){
				familycodestr = rs.getString("familycode");
			}
			if(familycodestr.equals(""))
				familycodestr="0000";
			int familycode = Integer.parseInt(familycodestr.substring(familycodestr.length()-4,familycodestr.length()));
			familycode++;
			this.familycode=this.group+String.format("%04d", familycode);
			
			System.out.println(familycode);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public void add() throws SQLException, ParseException {
		createFamilycode();
		if(this.holder.getFarmer().equals("否")){
			this.farmernum=0;
		}
		familydao.add(this);
	}

	public void del(String familycode) throws SQLException {
		familydao.del(familycode);
	}

	public void edit() throws SQLException {
		familydao.update(this);
	}

	public Family query() throws SQLException {
		return familydao.query(familycode);
	}

	public List<Family> queryAll() throws SQLException {
		return familydao.queryAll();
	}

	public PageBean queryAll(int page, int size) throws SQLException {
		return familydao.queryAll(page, size);
	}

	public String getFamilycode() {
		return familycode;
	}
	public void setFamilycode(String familycode) {
		this.familycode = familycode;
	}
	public String getFamilyattribute() {
		return familyattribute;
	}

	public void setFamilyattribute(String familyattribute) {
		this.familyattribute = familyattribute;
	}

	public int getFamilynum() {
		return familynum;
	}

	public void setFamilynum(int familynum) {
		this.familynum = familynum;
	}

	public int getFarmernum() {
		return farmernum;
	}

	public void setFarmernum(int farmernum) {
		this.farmernum = farmernum;
	}

	public String getTown() {
		return town;
	}

	public void setTown(String town) {
		this.town = town;
	}

	public String getVillage() {
		return village;
	}

	public void setVillage(String village) {
		this.village = village;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBuildtime() {
		return buildtime;
	}

	public void setBuildtime(Date buildtime) {
		this.buildtime = buildtime;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Person getHolder() {
		return holder;
	}

	public void setHolder(Person holder) {
		this.holder = holder;
	}

	public FamilyDao getFamilydao() {
		return familydao;
	}

	public void setFamilydao(FamilyDao familydao) {
		this.familydao = familydao;
	}

}
